﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

using System;
using System.Windows;
using System.Windows.Controls;

namespace Checkers.Client
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            if (App.Current.IsRunningOutOfBrowser)
            {
                mBackground.RadiusX = 0;
                mBackground.RadiusY = 0;
                mBoardMask.RadiusX = 0;
                mBoardMask.RadiusY = 0;
            }

            mTitleScreen.mStart.Click += new RoutedEventHandler(TitleScreenStart_Click);
            mTitleScreen.mCredits.Click += new RoutedEventHandler(TitleScreenCredits_Click);

            mPlayAgain.mYesButton.Click += new RoutedEventHandler(PlayAgainYes_Click);
            mPlayAgain.mNoButton.Click += new RoutedEventHandler(PlayAgainNo_Click);

            mBoard.GameModel.GameOverEvent += new Checkers.Common.Game.GameOverEventHandler(Board_GameOverEvent);

            mGameMenu.mNewGame.Click += new RoutedEventHandler(GameMenuNewGame_Click);
            mGameMenu.mQuit.Click += new RoutedEventHandler(GameMenuQuit_Click);

            mNewGame.mOkButton.Click += new RoutedEventHandler(NewGameOkButton_Click);
            mNewGame.mCancelButton.Click += new RoutedEventHandler(NewGameCancelButton_Click);

            mProduct.Text = App.Product;
            mVersion.Text = App.Version;
            mCopyright.Text = App.Copyright;
        }

        void TitleScreenStart_Click(object sender, RoutedEventArgs e)
        {
            mNewGame.ForceFadeIn();
            mTitleScreen.FadeOut();
        }

        void UpdateButtons()
        {
            mGameMenu.mNewGame.IsEnabled = false;
            mGameMenu.mQuit.IsEnabled = false;

            mBoardMask.Visibility = Visibility.Visible;

            if (mPlayAgain.Visibility == Visibility.Visible) return;

            mGameMenu.mNewGame.IsEnabled = true;
            mGameMenu.mQuit.IsEnabled = true;

            mBoardMask.Visibility = Visibility.Collapsed;
        }

        void TitleScreenCredits_Click(object sender, RoutedEventArgs e)
        {
            mCredits.FadeIn();
        }

        void GameMenuNewGame_Click(object sender, RoutedEventArgs e)
        {
            mBoard.Pause();

            mNewGame.FadeIn();
        }

        void GameMenuQuit_Click(object sender, RoutedEventArgs e)
        {
            mBoard.Reset();

            mTitleScreen.FadeIn();
        }

        void NewGameOkButton_Click(object sender, RoutedEventArgs e)
        {
            OnBeginGame();

            mNewGame.FadeOut();
        }

        void NewGameCancelButton_Click(object sender, RoutedEventArgs e)
        {
            mBoard.Resume();

            mNewGame.FadeOut();
        }

        void Board_GameOverEvent(object sender, Checkers.Common.Game.GameOverEventArgs e)
        {
            if (e.Winner == Checkers.Common.Game.Winner.Red)
                mPlayAgain.mMessage.Text = "Red player wins!";
            else if (e.Winner == Checkers.Common.Game.Winner.Black)
                mPlayAgain.mMessage.Text = "Black player wins!";
            else mPlayAgain.mMessage.Text = "It's a draw!";

            mBoard.DeselectTiles();

            mBoardMask.Visibility = Visibility.Visible;
            mPlayAgain.Visibility = Visibility.Visible;

            UpdateButtons();
        }

        void PlayAgainYes_Click(object sender, RoutedEventArgs e)
        {
            mPlayAgain.Visibility = Visibility.Collapsed;
            UpdateButtons();

            OnBeginGame();
        }

        void PlayAgainNo_Click(object sender, RoutedEventArgs e)
        {
            mPlayAgain.Visibility = Visibility.Collapsed;
            UpdateButtons();

            mBoard.Reset();

            mTitleScreen.FadeIn();
        }

        private void OnBeginGame()
        {
            bool robot1 = mNewGame.mRedComputer.IsChecked.Value;
            bool robot2 = mNewGame.mBlackComputer.IsChecked.Value;

            mBoard.BeginGame(robot1, robot2);
        }
    }
}
